package com.jx.smartbot.controller;

import java.net.URI;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.gocom.imoss.common.Exception.GoComException;
import com.jx.smartbot.VO.GoComResult;
import com.jx.smartbot.VO.Result;
import com.jx.smartbot.VO.group.GroupBatch;
import com.jx.smartbot.VO.group.GroupCreator;
import com.jx.smartbot.VO.group.GroupDescription;
import com.jx.smartbot.VO.group.GroupInfo;
import com.jx.smartbot.VO.group.GroupInfos;
import com.jx.smartbot.VO.group.GroupList;
import com.jx.smartbot.service.SystemSuperApp;
import com.jx.smartbot.util.JacksonUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@Api(value = "与群有关的API",description="透传gocomapi")
public class GroupApi extends BaseController{
	private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(GroupApi.class);
	@Resource(name="apiurl")
	String gocomeApiUrl;
	@Resource(name="defaultAppId")
	String defaultAppId;
	@Resource(name="defaultGroupAppId")
	String  defaultGroupAppId;
	@Autowired
	RestTemplate restTemplate;
	@Autowired
	SystemSuperApp systemSuperApp;
	
	@ApiOperation(value = "新建群组", notes = "新建群组，必须至少有一个用户")  
	@RequestMapping(value = "groups/creat", method = RequestMethod.POST)
	@ResponseStatus(HttpStatus.OK)
	public GroupCreator groups_add(
			@RequestBody  GroupInfo groupInfo,
			HttpServletRequest request) {
		logger.info("GroupCreator:{}",systemSuperApp.appid_token(defaultGroupAppId));
		//logger.info("GroupCreator:",systemSuperApp.appid_token(defaultGroupAppId));
		String rt = restTemplate.postForObject(gocomeApiUrl + "groupapp/group/new?"+systemSuperApp.appid_token(defaultGroupAppId),groupInfo,String.class);
		logger.info("GroupCreator:{}",rt);
		Object rt_object=JacksonUtil.readValue(rt, Object.class);
		if(rt_object instanceof GroupCreator){
			return (GroupCreator)rt_object;
		}
		throw new GoComException(rt);
	}
	
	@ApiOperation(value = "编辑群组", notes = "")  
	@RequestMapping(value = "groups/edit", method = RequestMethod.PUT)
	@ResponseStatus(HttpStatus.OK)
	public GoComResult groups_edit(
			//@PathVariable("groupid") String groupid,
			@RequestBody  GroupInfos groupInfos,
			HttpServletRequest request) {
		GoComResult result = restTemplate.postForObject(gocomeApiUrl + "groupapp/group/update?"+systemSuperApp.appid_token(defaultGroupAppId),groupInfos,GoComResult.class);
		return result;
	}
	
	@ApiOperation(value = "解散群组", notes = "通过群ID解散")  
	@RequestMapping(value = "groups/dismiss/{groupid}", method = RequestMethod.PUT)
	@ResponseStatus(HttpStatus.OK)
	public GoComResult groups_dismiss(
			@PathVariable("groupid") String groupid,
			HttpServletRequest request) {
		String gs="{\"groupid\":\""+groupid+"\"}";
		GoComResult result = restTemplate.postForObject(gocomeApiUrl + "groupapp/group/dismiss?"+systemSuperApp.appid_token(defaultGroupAppId),gs,GoComResult.class);
		return result;
	}
	
	@ApiOperation(value = "获取群组成员列表", notes = "")  
	@RequestMapping(value = "group/member/{groupid}", method = RequestMethod.GET)
	@ResponseStatus(HttpStatus.OK)
	public List<GroupInfo.Groupuser> group_member(
			@PathVariable String groupid,
			HttpServletRequest request) {
		String urlString=gocomeApiUrl + "groupapp/group/member/list?"+systemSuperApp.appid_token(defaultGroupAppId)+"&groupid="+groupid;
		URI uri = URI.create(urlString);   
		String rt=restTemplate.getForObject(uri,String.class);
		Object object=JacksonUtil.readValue(rt, Object.class);
		if(object instanceof  List ){
			@SuppressWarnings("unchecked")
			List<GroupInfo.Groupuser> member=(List<GroupInfo.Groupuser>)object;
			return  member;
		}
		throw new GoComException(rt);
	}
	
	@ApiOperation(value = "更新管理员", notes = "")  
	@RequestMapping(value = "group/member/{groupid}/update", method = RequestMethod.PUT)
	@ResponseStatus(HttpStatus.OK)
	public GoComResult group_member_edit(
			@PathVariable String groupid,
			@RequestBody  List<GroupInfo.Groupuser> groupusers,
			HttpServletRequest request) {
		String urlString=gocomeApiUrl + "groupapp/group/manager/update?"+systemSuperApp.appid_token(defaultGroupAppId)+"&groupid="+groupid;
		GoComResult result  = restTemplate.postForObject(urlString,groupusers,GoComResult.class);
		return result;
	}
	
	@ApiOperation(value = "添加群组成员", notes = "")  
	@RequestMapping(value = "group/member/{groupid}/add", method = RequestMethod.POST)
	@ResponseStatus(HttpStatus.OK)
	public GoComResult group_member_add(
			@PathVariable String groupid,
			@RequestBody  List<GroupInfo.Groupuser> groupusers,
			HttpServletRequest request) {
		String urlString=gocomeApiUrl + "groupapp/group/member/add?"+systemSuperApp.appid_token(defaultGroupAppId)+"&groupid="+groupid;
		//logger.info(urlString);
		//logger.info(restTemplate.postForObject(urlString,groupusers,String.class));
		/*String rt= restTemplate.postForObject(urlString,groupusers,String.class);
		Object object=JacksonUtil.readValue(rt, Object.class);
		if(object instanceof  GroupList ){
			return (GroupList)object;
		}
		throw new GoComException(rt);*/
		//GoComResult result = restTemplate.postForObject(urlString,groupusers,GoComResult.class);
		//GoComResult result = JacksonUtil.readValue(rt, GoComResult.class);
		String rt= restTemplate.postForObject(urlString,groupusers,String.class);
		logger.info(rt);
		GoComResult object=JacksonUtil.readValue(rt, GoComResult.class);
		
		return object;
	}
	
	@ApiOperation(value = "移除群组成员", notes = "")  
	@RequestMapping(value = "group/member/{groupid}/remove", method = RequestMethod.DELETE)
	@ResponseStatus(HttpStatus.OK)
	public GoComResult group_member_remove(
			@PathVariable String groupid,
			@RequestBody  List<GroupInfo.Groupuser> groupusers,
			HttpServletRequest request) {
		String urlString=gocomeApiUrl + "groupapp/group/member/remove?"+systemSuperApp.appid_token(defaultGroupAppId)+"&groupid="+groupid;
		String rt= restTemplate.postForObject(urlString,groupusers,String.class);
		GoComResult object=JacksonUtil.readValue(rt, GoComResult.class);
		//Object object=JacksonUtil.readValue(rt, Object.class);
		//GoComResult result = restTemplate.postForObject(urlString,groupusers,GoComResult.class);
		return object;
	}
	
	@ApiOperation(value = "获取群组列表", notes = "一次拉取调用最多拉取10000个关注者的GROUPID， 可以通过多次拉取的方式来满足需求.")  
	@RequestMapping(value = "groups", method = RequestMethod.GET)
	@ResponseStatus(HttpStatus.OK)
	public GroupList groups(
			@ApiParam(required=false)@RequestParam(value="next_groupid", defaultValue="") String next_groupid,
			HttpServletRequest request) {
		String urlString=gocomeApiUrl + "groupapp/group/list?"+systemSuperApp.appid_token(defaultGroupAppId);//+"&next_groupid="+next_groupid;
		logger.info(urlString);
		if(next_groupid.equals("")){
			urlString=urlString+"&next_groupid="+next_groupid;
		}
		URI uri = URI.create(urlString);   
		String rt=restTemplate.getForObject(uri,String.class);
		Object object=JacksonUtil.readValue(rt, Object.class);
		if(object instanceof  GroupList ){
			return (GroupList)object;
		}
		throw new GoComException(rt);
	}
	
	@ApiOperation(value = "获取群组基本信息", notes = "获取群组基本信息")  
	@RequestMapping(value = "groupinfo/{groupid}", method = RequestMethod.GET)
	@ResponseStatus(HttpStatus.OK)
	public GroupDescription groupInfo(
			//@RequestParam(value="groupid", defaultValue="") String groupid,
			@PathVariable("groupid") String groupid,
			HttpServletRequest request) {
		String urlString=gocomeApiUrl + "groupapp/group/info?"+systemSuperApp.appid_token(defaultGroupAppId)+"&groupid="+groupid;
		URI uri = URI.create(urlString);   
		String rt=restTemplate.getForObject(uri,String.class);
		Object object=JacksonUtil.readValue(rt, Object.class);
		if(object instanceof  GroupDescription ){
			return (GroupDescription)object;
		}
		throw new GoComException(rt);
	}
	
	@ApiOperation(value = "批量获取群组基本信息", notes = "批量获取群组基本信息")  
	@RequestMapping(value = "groupinfos/batchget", method = RequestMethod.POST)
	@ResponseStatus(HttpStatus.OK)
	public List<GroupDescription> batchget(//GroupBatch
			//@RequestParam(value="groupid", defaultValue="") String groupid,
			@RequestBody  List<String> groupid,
			HttpServletRequest request) {
		String urlString=gocomeApiUrl + "groupapp/group/info/batchget?"+systemSuperApp.appid_token(defaultGroupAppId);
		String  rt = restTemplate.postForObject(urlString,groupid,String.class);
		Object object=JacksonUtil.readValue(rt, Object.class);
		if(object instanceof  GroupList ){
			GroupBatch gl=(GroupBatch)object;
			return gl.getGroup_info_list();
		}
		throw new GoComException(rt);
	}
}
